home *** CD-ROM | disk | FTP | other *** search
/ Ham Radio 2000 #2 / Ham Radio 2000 - Volume 2.iso / HAMV2 / TCP_IP / TNOS230S / STATS_F.C < prev    next >
Encoding:
C/C++ Source or Header  |  1997-07-30  |  10.6 KB  |  470 lines

  1. #include "global.h"
  2. #ifdef STATS_FWD
  3. #include "stats.h"
  4. #include "stats_f.h"
  5. #ifdef MSDOS
  6. #include "ctype.h"
  7. #endif
  8.  
  9. #if !defined(_lint)
  10. static char rcsid[] OPTIONAL = "$Id: stats_f.c,v 1.12 1997/07/31 00:44:20 root Exp root $";
  11. #endif
  12.  
  13. static int STATdaily_fwd (int argc,char *argv[],void *p);
  14. static int STATweekly_fwd (int argc,char *argv[],void *p);
  15. static int STATmonthly_fwd (int argc,char *argv[],void *p);
  16. static int STATyearly_fwd (int argc,char *argv[],void *p);
  17. static int STATgeneral_fwd (int argc,char *argv[],void *p);
  18. static int STATlist_fwd (int argc,char *argv[],void *p);
  19.  
  20. static struct fwd_stats fwdstats;
  21.  
  22.  
  23. static struct fwd_data *sanity_check (int argc,char *argv[],void *p);
  24. static struct fwd_data *find_entry (char *name);
  25. static struct fwd_data *create_entry (char *name, int add);
  26.  
  27.  
  28. static const char fwddailyhdr[] = "%8.8s%-35.35sOUTGOING\n\n";
  29. static const char fwdweeklyhdr[] = "%12.12s%-40.40sOUTGOING\n\n";
  30. static const char incomingstr[] = "INCOMING";
  31. static const char fwdwithstr[] = "Forwarding with %s";
  32. static const char fwdmonthlyhdr[] = "%12.12s%-42.42sOUTGOING\n\n";
  33. static const char fwdyearlyhdr[] = "%2.2s%-23.23sOUTGOING\n\n";
  34.  
  35.  
  36. static struct cmds STATSfwdcmds[] = {
  37.     { "clear",    doSTATclear_fwd,0,    0,    NULLCHAR },
  38.     { "daily",    STATdaily_fwd,    0,    0,    NULLCHAR },
  39.     { "general",    STATgeneral_fwd,0,     0,    NULLCHAR },
  40.     { "list",    STATlist_fwd,    0,     0,    NULLCHAR },
  41.     { "monthly",    STATmonthly_fwd,0,     0,    NULLCHAR },
  42.     { "weekly",    STATweekly_fwd,    0,     0,    NULLCHAR },
  43.     { "yearly",    STATyearly_fwd,    0,     0,    NULLCHAR },
  44.     { NULLCHAR,    NULL,        0,    0,    NULLCHAR }
  45. };
  46.  
  47.  
  48.  
  49. int
  50. doSTATforward(argc,argv,p)
  51. int argc;
  52. char *argv[];
  53. void *p;
  54. {
  55. char *temp;
  56.  
  57.     if (argc == 3)    {
  58.         temp = argv[1];
  59.         argv[1] = argv[2];
  60.         argv[2] = temp;
  61.     } else if (argc == 2 && tolower(argv[1][0]) != 'l' )
  62.         tprintf ("Syntax: stats forwarding <bbsname> <subcommand>\n\n");
  63.     return subcmd(STATSfwdcmds,argc,argv,p);
  64. }
  65.  
  66.  
  67. static struct fwd_data *
  68. find_entry (name)
  69. char *name;
  70. {
  71. struct fwd_data *fd;
  72.  
  73.     fd = fwdstats.head;
  74.     while (fd)    {
  75.         if (!stricmp (name, fd->name))
  76.             return (fd);
  77.         fd = fd->next;
  78.     }
  79.     return ((struct fwd_data *)-1);
  80. }
  81.  
  82.  
  83. static struct fwd_data *
  84. create_entry (name, add)
  85. char *name;
  86. int add;
  87. {
  88. struct fwd_data *fd;
  89.  
  90.     fd = find_entry (name);
  91.     if (fd == (struct fwd_data *)-1)    {
  92.         fd = callocw (1, sizeof (struct fwd_data)); /* no error checking, yet */
  93.         if (fd != (struct fwd_data *)0)    {
  94.             fd->name = strdup (name);
  95.             (void) strupr (fd->name);
  96.             fd->next = fwdstats.head;
  97.             fwdstats.head = fd;
  98.             if (add)
  99.                 fwdstats.count++;
  100.         }
  101.     }
  102.     return (fd);
  103. }
  104.  
  105.  
  106. static struct fwd_data *
  107. sanity_check(argc,argv,p)
  108. int argc;
  109. char *argv[];
  110. void *p OPTIONAL;
  111. {
  112. struct fwd_data *fd;
  113.  
  114.     if (argc < 2)    {
  115.         tprintf ("This command requires both a 'bbsname' and a subcommand\n");
  116.         return ((struct fwd_data *)-1);
  117.     }
  118.     
  119.     fd = find_entry(argv[1]);
  120.     if (fd == (struct fwd_data *)-1)
  121.         tprintf ("Statistics data for that BBS not found\n");
  122.     return (fd);
  123. }
  124.  
  125.  
  126. int
  127. doSTATclear_fwd(argc,argv,p)
  128. int argc OPTIONAL;
  129. char *argv[] OPTIONAL;
  130. void *p OPTIONAL;
  131. {
  132. struct fwd_data *fd, *temp;
  133.  
  134.     if(Curproc->input != Command->input)
  135.         tputs (STAT_cannotclear);
  136.     else    {
  137.         fd = fwdstats.head;
  138.         while (fd)    {
  139.             free (fd->name);
  140.             temp = fd;
  141.             fd = fd->next;
  142.             free (temp);
  143.         }
  144.  
  145.         memset (&fwdstats, 0, sizeof (struct fwd_stats));
  146.         fwdstats.days = 1;
  147.         fwdstats.start = time((time_t *)0);
  148.         savestats_fwd();
  149.         log (-1, "Clearing forward stats");
  150.     }
  151.     return 0;
  152. }
  153.  
  154.  
  155. static int
  156. STATlist_fwd(argc,argv,p)
  157. int argc OPTIONAL;
  158. char *argv[] OPTIONAL;
  159. void *p OPTIONAL;
  160. {
  161. struct fwd_data *fd;
  162. int theindex = 0;
  163.  
  164.     tputs ("BBSs with defined statistics:\n\n");
  165.     fd = fwdstats.head;
  166.     while (fd)    {
  167.         tprintf ("%-19.19s", fd->name);
  168.         if (!(++theindex % 4))
  169.             tputc ('\n');
  170.         fd = fd->next;
  171.     }
  172.     tputc ('\n');
  173.     if (theindex % 4)
  174.         tputc ('\n');
  175.     return 0;
  176. }
  177.  
  178.  
  179. static int
  180. STATgeneral_fwd(argc,argv,p)
  181. int argc;
  182. char *argv[];
  183. void *p;
  184. {
  185. char buf1[45], buf2[26], buf3[36];
  186. int temp, l;
  187. int highest[2], second[2];
  188. struct fwd_data *fd;
  189.  
  190.     if ((fd = sanity_check (argc, argv, p)) == (struct fwd_data *)-1)
  191.         return 0;
  192.  
  193.     sprintf (buf1, "FORWARDING with %s", fd->name);
  194.     tprintf ("\n%26.26s: %-26.26s%-24.24s\n", buf1, incomingstr, "OUTGOING");
  195.     sprintf (buf1, "%ld", fd->messages[0]);
  196.     sprintf (buf2, "%ld", fd->messages[1]);
  197.     sprintf (buf3, "Messages since %6.6s", &(ctime(&fwdstats.start))[4]);
  198.     tprintf ("  %24.24s: %-26.26s%-24.24s\n", buf3, buf1, buf2);
  199.     sprintf (buf1, "%ld", fd->day[0]);
  200.     sprintf (buf2, "%ld", fd->day[1]);
  201.     tprintf ("  %24.24s: %-26.26s%-24.24s\n", "Messages since midnight",
  202.         buf1, buf2);
  203.     sprintf (buf1, "%ld", fd->hour[0]);
  204.     sprintf (buf2, "%ld", fd->hour[1]);
  205.     tprintf ("  %24.24s: %-26.26s%-24.24s\n", "Messages this hour",
  206.         buf1, buf2);
  207.     sprintf (buf1, "%d", (fwdstats.days > 1) ? (int)((fd->messages[0] - fd->day[0] + (fwdstats.days - 2)) / (fwdstats.days - 1)) : (int)fd->messages[0]);
  208.     sprintf (buf2, "%d", (fwdstats.days > 1) ? (int)((fd->messages[1] - fd->day[1] + (fwdstats.days - 2)) / (fwdstats.days - 1)) : (int)fd->messages[1]);
  209.     tprintf ("  %24.24s: %-26.26s%-24.24s\n", "Average messages per day",
  210.         buf1, buf2);
  211.     highest[0] = highest[1] = -1;
  212.     second[0] = second[1] = -1;
  213.     for (temp = 0; temp < 24; temp++)
  214.         for (l = 0; l < 2; l++)    {
  215.             if (highest[l] == -1 || fd->hourly[temp][l] > fd->hourly[highest[l]][l])    {
  216.                 second[l] = highest[l];
  217.                 highest[l] = temp;
  218.             } else if (second[l] == -1 || fd->hourly[temp][l] > fd->hourly[second[l]][l])
  219.                 second[l] = temp;
  220.         }
  221.     sprintf (buf1, "At %02d and %02d o'clock", highest[0], second[0]);
  222.     sprintf (buf2, "At %02d and %02d o'clock", highest[1], second[1]);
  223.     tprintf ("%26.26s: %-26.26s%-24.24s\n\n", "Rush hours", buf1, buf2);
  224.     return 0;
  225. }
  226.  
  227.  
  228.  
  229. static int
  230. STATdaily_fwd(argc,argv,p)
  231. int argc;
  232. char *argv[];
  233. void *p;
  234. {
  235. char buf[45];
  236. struct fwd_data *fd;
  237.  
  238.     if ((fd = sanity_check (argc, argv, p)) == (struct fwd_data *)-1)
  239.         return 0;
  240.     sprintf (buf, fwdwithstr, fd->name);
  241.     tprintf (dailyhdr, buf);
  242.     tprintf (fwddailyhdr, STAT_emptystr,incomingstr);
  243.     doGraph (24, fd->hourly);
  244.     tputs (STAT_dailytrailer);
  245.     return 0;
  246. }
  247.  
  248.  
  249. static int
  250. STATweekly_fwd(argc,argv,p)
  251. int argc;
  252. char *argv[];
  253. void *p;
  254. {
  255. char buf[45];
  256. struct fwd_data *fd;
  257.  
  258.     if ((fd = sanity_check (argc, argv, p)) == (struct fwd_data *)-1)
  259.         return 0;
  260.     sprintf (buf, fwdwithstr, fd->name);
  261.     tprintf (weeklyhdr, buf);
  262.     tprintf (fwdweeklyhdr, STAT_emptystr,incomingstr);
  263.     doGraph (7, fd->daily);
  264.     tputs (STAT_weeklytrailer);
  265.     return 0;
  266. }
  267.  
  268.  
  269. static int
  270. STATmonthly_fwd(argc,argv,p)
  271. int argc;
  272. char *argv[];
  273. void *p;
  274. {
  275. char buf[45];
  276. struct fwd_data *fd;
  277.  
  278.     if ((fd = sanity_check (argc, argv, p)) == (struct fwd_data *)-1)
  279.         return 0;
  280.     sprintf (buf, fwdwithstr, fd->name);
  281.     tprintf (monthlyhdr, buf);
  282.     tprintf (fwdmonthlyhdr, STAT_emptystr,incomingstr);
  283.     doGraph (31, fd->monthly);
  284.     tputs (STAT_monthlytrailer);
  285.     return 0;
  286. }
  287.  
  288.  
  289. static int
  290. STATyearly_fwd(argc,argv,p)
  291. int argc;
  292. char *argv[];
  293. void *p;
  294. {
  295. char buf[45];
  296. struct fwd_data *fd;
  297.  
  298.     if ((fd = sanity_check (argc, argv, p)) == (struct fwd_data *)-1)
  299.         return 0;
  300.     sprintf (buf, fwdwithstr, fd->name);
  301.     tprintf (yearlyhdr, buf);
  302.     tprintf (fwdyearlyhdr, STAT_emptystr,incomingstr);
  303.     doGraph (12, fd->yearly);
  304.     tputs (STAT_yearlytrailer);
  305.     return 0;
  306. }
  307.  
  308.  
  309. void
  310. STATS_addfwd (int which, int amount, char *name)
  311. {
  312. struct fwd_data *fd;
  313.  
  314.     if (which < 0 || which > 1)
  315.         return;
  316.     fd = create_entry (name, 1);
  317.     fd->messages[which] += amount;
  318.     fd->hour[which] += amount;
  319.     fd->day[which] += amount;
  320.     fd->month[which] += amount;
  321. }
  322.  
  323.  
  324. void
  325. updatestats_fwd (void)
  326. {
  327.     /* nothing, at this time - addfwdstats() does all the work */
  328. }
  329.  
  330.  
  331. void
  332. loadstats_fwd(void)
  333. {
  334. FILE *fp;
  335. char buffer[256];
  336. struct fwd_data *fd;
  337. int count;
  338. int len;
  339.  
  340.     sprintf (buffer, "%s/forward.dat", STATSDir);
  341.     fp = fopen (buffer, "r");
  342.     if (fp != NULLFILE)    {
  343.         (void) fread (&fwdstats, sizeof (struct fwd_stats) - sizeof(struct fwd_data *), 1, fp);
  344.         for (count = 0; count < fwdstats.count; count++)    {
  345.             len = fgetc (fp);
  346.             (void) fread (buffer, (unsigned) len, 1, fp);
  347.             buffer[len] = 0;
  348.             fd = create_entry (buffer, 0);
  349.             (void) fread (fd->messages, sizeof (struct fwd_data) - (sizeof(char *) + sizeof(struct fwd_data *)), 1, fp);    /*lint !e419 */
  350.         }
  351.         (void) fclose (fp);
  352.     }
  353. }
  354.  
  355.  
  356. void
  357. savestats_fwd(void)
  358. {
  359. FILE *fp;
  360. char buffer[256];
  361. struct fwd_data *fd;
  362. int count;
  363.  
  364.     sprintf (buffer, "%s/forward.dat", STATSDir);
  365.     fp = fopen (buffer, "w");
  366.     if (fp != NULLFILE)    {
  367.         (void) fwrite (&fwdstats, sizeof (struct fwd_stats) - sizeof(struct fwd_data *), 1, fp);
  368.         fd = fwdstats.head;
  369.         for (count = 0; count < fwdstats.count; count++, fd = fd->next)    {
  370.             fprintf (fp, "%c%s", strlen (fd->name), fd->name);
  371.             (void) fwrite (fd->messages, sizeof (struct fwd_data) - (sizeof(char *) + sizeof(struct fwd_data *)), 1, fp);    /*lint !e420 */
  372.         }
  373.         (void) fclose (fp);
  374.     } else
  375.         log (-1, "Can't open stats file '%s/forward.dat'", STATSDir);
  376. }
  377.  
  378.  
  379. void
  380. newhour_fwd (int hour)
  381. {
  382. struct fwd_data *fd;
  383.  
  384.     fd = fwdstats.head;
  385.     while (fd)    {
  386.         fd->hourly[hour][0] = fd->hour[0];
  387.         fd->hourly[hour][1] = fd->hour[1];
  388.         fd->hour[0] = fd->hour[1] = 0;
  389.         fd = fd->next;
  390.     }
  391. }
  392.  
  393.  
  394. void
  395. newday_fwd (int day)
  396. {
  397. struct fwd_data *fd;
  398.  
  399.     fd = fwdstats.head;
  400.     while (fd)    {
  401.         fd->daily[day][0] = fd->day[0];
  402.         fd->daily[day][1] = fd->day[1];
  403.         fd = fd->next;
  404.     }
  405. }
  406.  
  407.  
  408. void
  409. endmonthclear_fwd (int day, int month)
  410. {
  411. int k;
  412. struct fwd_data *fd;
  413. FILE *fp;
  414.  
  415.     fd = fwdstats.head;
  416.     fp = tmpfile();
  417.     if (fp)
  418.         fprintf (fp, "Monthly Forwarding Stats for the month of %s: %s\n\n", Months[month], Hostname);
  419.     while (fd)    {
  420.         /* clear out non-existent days of last month */
  421.         for (k = day; k < 31; k++)
  422.             fd->monthly[k][0] = fd->monthly[k][1] = 0L;
  423.         fd->yearly[month][0] = fd->month[0];
  424.         fd->yearly[month][1] = fd->month[1];
  425.         if (fp)
  426.             fprintf (fp, "[%s] Incoming: %ld\n[%s] Outgoing: %ld\n", fd->name, fd->month[0], fd->name, fd->month[1]);
  427.         fd->month[0] = fd->month[1] = 0;
  428.         fd = fd->next;
  429.     }
  430.     if (fp)    {
  431.         rewind (fp);
  432.         (void) rdaemon (fp, NULLCHAR, NULLCHAR, "sysop", "Monthly Forwarding Stats", 'P', 0);
  433.         (void) fclose (fp);
  434.     }
  435. }
  436.  
  437.  
  438. void
  439. endday_fwd (int day)
  440. {
  441. struct fwd_data *fd;
  442.  
  443.     fd = fwdstats.head;
  444.     while (fd)    {
  445.         fd->monthly[day][0] = fd->day[0];
  446.         fd->monthly[day][1] = fd->day[1];
  447.         fd->day[0] = fd->day[1] = 0;
  448.         fd = fd->next;
  449.     }
  450.     fwdstats.days++;
  451. }
  452.  
  453.  
  454. void
  455. eachcycle_fwd (time_t now)
  456. {
  457.     fwdstats.last = now;
  458. }
  459.  
  460.  
  461. void
  462. init_fwd (time_t now)
  463. {
  464.     fwdstats.days = 1;
  465.     fwdstats.start = now;
  466. }
  467.  
  468.  
  469. #endif /* STATS_FWD */
  470.